package com.easy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.*;

import com.easy.bean.*;
import com.easy.service.*;
import com.easy.util.*;

@RestController
@RequestMapping("/admin")
@CrossOrigin
public class AdminController {

    @Autowired
    private AdminService adminService;



    @GetMapping
    public Result getAll() {
        return Result.success(adminService.list());
    }

    @GetMapping("page")
    public Result getPage(Page page, @RequestParam(name = "searchtext", required = false) String searchtext) {
        QueryWrapper wrapper =null;
        if (searchtext != null && !searchtext.isEmpty()) {
            wrapper = new QueryWrapper<>();
            wrapper.like("name", searchtext);
        }
        return Result.success(adminService.page(page, wrapper));
    }
    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Long id) {
        return Result.success(adminService.getById(id));
    }

    @PostMapping
    public Result create(@RequestBody Admin entity) {
        return Result.success(adminService.save(entity));
    }

    @PutMapping
    public Result update( @RequestBody Admin entity) {
        return Result.success(adminService.save(entity));
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Long id) {
        return Result.success(adminService.removeById(id));
    }
}
